package com.myself.cloud.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myself.cloud.api.IApproveProcessConfigService;
import com.myself.cloud.entity.ResultEntity;
import com.myself.cloud.entity.bean.ApproveProcessConfig;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 审批流配置表 前端控制器
 *
 * @author baijialiang
 * @since 2022-07-04
 */

@RestController
@Api(value = "审批流配置表", tags = "审批流配置表")
@RequestMapping("/analyse/approve-process-config")
@Slf4j
public class ApproveProcessConfigController {
    @Autowired
    private IApproveProcessConfigService approveProcessConfigService;

    @RequestMapping("/list")
    @ApiOperation(value = "审批流配置表列表查询", notes = "list")
    @HystrixCommand(
            // 线程池标识，要保持唯⼀，不唯⼀的话就共⽤了
            threadPoolKey = "findResumeOpenStateTimeoutFallback", // 线程池细节属性配置
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "2"), // 线程数
                    @HystrixProperty(name = "maxQueueSize", value = "20") // 等待队列⻓度
            },
            // commandProperties熔断的⼀些细节属性配置
            commandProperties = {
                    // 每⼀个属性都是⼀个HystrixProperty
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "2000"),
                    // hystrix⾼级配置，定制⼯作过程细节
                    // 统计时间窗⼝定义
                    @HystrixProperty(name =
                            "metrics.rollingStats.timeInMilliseconds", value = "8000"),
                    // 统计时间窗⼝内的最⼩请求数
                    @HystrixProperty(name =
                            "circuitBreaker.requestVolumeThreshold", value = "2"),
                    // 统计时间窗⼝内的错误数量百分⽐阈值
                    @HystrixProperty(name =
                            "circuitBreaker.errorThresholdPercentage", value = "50"),
                    // ⾃我修复时的活动窗⼝⻓度
                    @HystrixProperty(name =
                            "circuitBreaker.sleepWindowInMilliseconds", value = "3000")
            }/*,
            fallbackMethod = "myFallBack" // 回退⽅法*/
    )
    public ResultEntity<List<ApproveProcessConfig>> list(@RequestBody ApproveProcessConfig approveProcessConfig) {
        // 模拟异常或者sleep
        //int a = 1/0;
        QueryWrapper<ApproveProcessConfig> queryWrapper = new QueryWrapper<>(approveProcessConfig);
        // 自己补写条件
        return ResultEntity.success(approveProcessConfigService.list(queryWrapper));
    }

    public ResultEntity<List<ApproveProcessConfig>> myFallBack(ApproveProcessConfig approveProcessConfig) {
        return ResultEntity.success();
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "审批流配置表列表分页查询，pageSize、pageNum从请求头中获取", notes = "pageList")
    public ResultEntity<Page<ApproveProcessConfig>> pageList(@RequestBody ApproveProcessConfig approveProcessConfig,
                                                             @RequestHeader Integer pageSize,
                                                             @RequestHeader Integer pageNum) {
        QueryWrapper<ApproveProcessConfig> queryWrapper = new QueryWrapper<>(approveProcessConfig);
        // 自己补写条件
        Page<ApproveProcessConfig> page = new Page<>(pageNum, pageSize);
        return ResultEntity.success(approveProcessConfigService.page(page, queryWrapper));
    }

    @PostMapping("/detail")
    @ApiOperation(value = "审批流配置表查看详情", notes = "ApproveProcessConfig")
    public ResultEntity<ApproveProcessConfig> getApproveProcessConfigById(@RequestBody ApproveProcessConfig approveProcessConfig) {
        return ResultEntity.success(approveProcessConfigService.getById(approveProcessConfig));
    }

    @PostMapping("/save")
    @ApiOperation(value = "审批流配置表新增", notes = "ApproveProcessConfig")
    public ResultEntity<Boolean> saveApproveProcessConfig(@RequestBody ApproveProcessConfig approveProcessConfig) {
        if (approveProcessConfigService.save(approveProcessConfig)) {
            return ResultEntity.success();
        }
        return ResultEntity.error("");
    }

    @PostMapping("/update")
    @ApiOperation(value = "审批流配置表修改", notes = "ApproveProcessConfig")
    public ResultEntity<Boolean> update(@RequestBody ApproveProcessConfig approveProcessConfig) {
        if (approveProcessConfigService.updateById(approveProcessConfig)) {
            return ResultEntity.success();
        }
        return ResultEntity.error("");
    }

    @PostMapping("/remove")
    @ApiOperation(value = "审批流配置表删除", notes = "ApproveProcessConfig")
    public ResultEntity<Boolean> remove(@RequestBody ApproveProcessConfig approveProcessConfig) {
        if (approveProcessConfigService.removeById(approveProcessConfig)) {
            return ResultEntity.success();
        }
        return ResultEntity.error("");
    }
}
